<template>
  <div class="task-number-safeguard-container page-main">
    <query-page
      ref="taskQueryPage"
      v-model:selectedKeys="selectedKeys"
      title="任务编号维护"
      :form-component-config="formComponentConfig"
      :form-render-config="formRenderConfig"
      :form-render-keys="formRenderKeys"
      :table-component-config="tableComponentConfig"
      :table-render-config="tableRenderConfig"
      :table-render-keys="tableRenderKeys"
      :loading="isLoading"
      :data="renderData"
      :total="total"
      @selection-change="selectionChange"
      @query-data="getData"
    >
      <template #container-side>
        <a-button
          v-permission="['pms:order-task:save']"
          type="primary"
          @click="openAdd"
          >新增</a-button
        >
      </template>

      <template #operate="{ record }">
        <a-button
          v-permission="['pms:order-task:update']"
          type="text"
          @click="edit(record)"
          >编辑</a-button
        >
        <a-button
          v-permission="['pms:order-task:delete']"
          type="text"
          @click="del(record)"
          >删除</a-button
        >
      </template>
    </query-page>
    <addDialog
      v-if="addVisible"
      :row-data="rowData"
      @close-drawer="closeDrawer"
      @success="success"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';
  import { Message } from '@arco-design/web-vue';
  import queryPage from '@/common/components/query-page/index.vue';
  import { hasDataAndBack } from '@/common/utils/index';
  import { info } from '@/common/utils/modal-promise';
  import {
    formRenderConfig,
    formComponentConfig,
  } from '@/views/purchase-order/components/formComp/baseFormConfig';
  import {
    tableRenderConfig,
    tableComponentConfig,
  } from '@/views/purchase-order/components/tableComp/baseTableConfig';
  import {
    getPmsOrderTaskPage,
    delOrderTask,
  } from '@/api/purchase-order/taskNumberSafeguard';
  import addDialog from './add.vue';

  const selectedKeys = ref([]);
  const isLoading = ref(true);
  const renderData = ref<any[]>([]);
  const addVisible = ref(false);
  const approvalVisible = ref(false);
  const rowData = ref();
  const total = ref(0);
  const taskQueryPage = ref();
  // 渲染检索项
  const formRenderKeys = ref<any[]>(['taskNum', 'taskName']);

  // 当前表格要渲染哪些值
  const tableRenderKeys = ref<any[]>(['taskNum', 'taskName', 'operate']);

  // 添加
  const openAdd = () => {
    rowData.value = null;
    addVisible.value = true;
  };
  // 编辑
  const edit = (row: any) => {
    rowData.value = { ...row };
    addVisible.value = true;
  };
  // 删除
  const del = async (row: any) => {
    await info({
      title: '您确定要删除当前所选的数据？',
      content: '',
      hideCancel: false,
    });
    const res: any = await delOrderTask(row.id);
    if (res.code === 200) {
      Message.success('删除成功');
      getData(taskQueryPage.value.getStatus());
    }
  };
  const getData = async (queryInfo: object) => {
    renderData.value = [];
    isLoading.value = true;
    console.log('queryInfo', queryInfo);
    const res = (await getPmsOrderTaskPage(queryInfo)) as any;

    if (res.code === 200) {
      total.value = res.data.total;
      renderData.value = res.data.list;
      refreshNumData(renderData.value, total.value);
    }
    isLoading.value = false;
  };
  const refreshNumData = (list: any[], tNum: any) => {
    if (hasDataAndBack(list, tNum, taskQueryPage.value?.getStatus().pageNo)) {
      taskQueryPage.value?.setCurrent(
        taskQueryPage.value.getStatus().pageNo - 1
      );
      getData(taskQueryPage.value.getStatus());
    }
  };
  const selectionChange = (val: string[]) => {
    console.log('selectedKeys', val);
  };
  // 关闭弹窗
  const closeDrawer = () => {
    addVisible.value = false;
    approvalVisible.value = false;
  };
  // 关闭弹窗
  const success = () => {
    addVisible.value = false;
    getData(taskQueryPage.value.getStatus());
  };
</script>

<style lang="less" scoped>
  .task-number-safeguard-container {
    height: calc(100vh - 125px);
  }
</style>
